package com.zhanghe.mall.web.controller;

import com.zhanghe.mall.service.RoleService;
import com.zhanghe.mall.service.dto.FindRoleDTO;
import com.zhanghe.mall.service.dto.out.PermissionsDTO;
import com.zhanghe.mall.service.dto.out.RoleDTO;
import com.zhanghe.mall.web.po.AddRolePO;
import com.zhanghe.mall.web.po.FindRolePO;
import com.zhanghe.mall.web.util.R;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RoleController {

  @Autowired
  private RoleService roleService;

  /**
   * 获取全部角色列表
   * @return
   */
  @GetMapping("/role")
  public R allRoles(FindRolePO findRolePO){
    FindRoleDTO findRoleDTO = new FindRoleDTO();
    BeanUtils.copyProperties(findRolePO,findRoleDTO);
    Page<RoleDTO> result = roleService.findByPage(findRoleDTO);
    return new R(result);
  }

  @PostMapping("/role")
  public R addRoles(AddRolePO addRolePO){
    return new R(1);
  }

  /**
   * 获取角色权限列表
   * @return
   */
  @GetMapping("/role/{roleId}/permissions")
  public R rolePermissions(@PathVariable Long roleId){
    List<PermissionsDTO> list = roleService.findRolePermissions(roleId);
    return new R(list);
  }

}
